package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.EnumUtil;
import com.ruoyi.system.domain.Approval;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.enums.AuditStateEnum;
import com.ruoyi.system.enums.DocTypeEnum;
import com.ruoyi.system.service.IApprovalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ContractMapper;
import com.ruoyi.system.domain.Contract;
import com.ruoyi.system.service.IContractService;
import com.ruoyi.common.core.text.Convert;

/**
 * 合同Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-04-13
 */
@Service
public class ContractServiceImpl implements IContractService 
{
    @Autowired
    private ContractMapper contractMapper;
    @Autowired
    private IApprovalService approvalService;

    /**
     * 查询合同
     * 
     * @param id 合同ID
     * @return 合同
     */
    @Override
    public Contract selectContractById(Long id)
    {
        return contractMapper.selectContractById(id);
    }

    /**
     * 查询合同列表
     * 
     * @param contract 合同
     * @return 合同
     */
    @Override
    public List<Contract> selectContractList(Contract contract)
    {
        return contractMapper.selectContractList(contract);
    }

    /**
     * 新增合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int insertContract(Contract contract)
    {
        return contractMapper.insertContract(contract);
    }

    /**
     * 修改合同
     * 
     * @param contract 合同
     * @return 结果
     */
    @Override
    public int updateContract(Contract contract)
    {
        return contractMapper.updateContract(contract);
    }

    /**
     * 删除合同对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteContractByIds(String ids)
    {
        return contractMapper.deleteContractByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除合同信息
     * 
     * @param id 合同ID
     * @return 结果
     */
    @Override
    public int deleteContractById(Long id)
    {
        return contractMapper.deleteContractById(id);
    }

    @Override
    public void applyAutdit(Contract writings, SysUser sysUser) {
        writings.setAuditState(AuditStateEnum.AUDIT_ING.getValue());
        updateContract(writings);
        Approval approval = new Approval();
        approval.setApplyManId(sysUser.getUserId());
        approval.setApplyMan(sysUser.getUserName());
        approval.setApplyManDept(writings.getDepartmentId());
        Date now = new Date();
        approval.setApplyTime(now);
        StringBuilder sb = new StringBuilder();
        String docType = EnumUtil.getIEnum(DocTypeEnum.class,writings.getDocType()).getDesp();
        sb.append(sysUser.getUserName())
                .append(DateUtils.parseDateToStr("yyyy-MM-dd HH:mm",now))
                .append("【")
                .append(writings.getContractName())
                .append("<")
                .append(docType)
                .append("申办")
                .append(">")
                .append("】")
                .append("的申请");
        approval.setApprovalName(sb.toString());
        approval.setDocType(writings.getDocType());
        approval.setDocRelationId(writings.getId());
        approval.setApprovalResult(AuditStateEnum.NOT_AUDIT.getValue());
        approvalService.insertApproval(approval);
    }
}
